# -*- coding: utf-8 -*-
"""
Created on Sat Jul 15 17:28:55 2017

@author: ahchpr

E-mail: hellochenpro@hotmail.com

"""

import os
import time
import xlwt


"""从日志源数据，获取每个ip曾经被记录过的ua信息，以字典的形式返回结果
"""
def fetch_source_ip(file_name):
    start = time.clock()
    alist = []
    adict = {}
    
    with open(file_name, 'r', encoding='utf-8') as fd:
        for line in fd:
            alist = [item for item in line.strip().split("\"") if item ]
            if len(alist) == 0:
                continue
            ip = alist[0].split(" ")[0].strip()
            tmp = alist[-1]
            ua = tmp
            if ip not in adict :
                adict[ip] = [ua]
            elif ip in adict:
                if ua not in adict[ip]:
                    adict[ip].append(ua)
            del ip, tmp
    end = time.clock()
    print("time: {}".format(end - start))
    return (adict)


""" 获取需要查找的ip列表
"""
def fetch_check_ip(file_name):
    alist = []
    with open(file_name, 'r', encoding='utf-8') as fd:
        for line in fd:
            if line.strip():
                alist.append(line.strip())
    return (alist)


"""把最后统计的结果保存到xls里
"""
def  save_to_xls(result_dict, n_set):                              
  style_normal = xlwt.XFStyle()
  font = xlwt.Font()
  font.name = 'SimSun'
  style_normal.font = font
  
  style_yellowBg = xlwt.easyxf('pattern: pattern solid, fore_colour yellow; font: bold on;');
      
  i = 0  
  book = xlwt.Workbook(encoding='utf-8', style_compression=0)
  sheet = book.add_sheet('result_ip', cell_overwrite_ok=True)   
  for ip, v_all_ua in result_dict.items():
    
    sheet.write(i, 0, ip)
    num = len(v_all_ua)
    for j in range(1, num+1):
      sheet.write(i, j, v_all_ua[j-1], style_normal)
    i = i+1
  for item in n_set:
      sheet.write(i, 0, item, style_yellowBg)
      i = i+1
      
  xl_name = "result_ip.xls" 
  book.save(xl_name)


if __name__ == "__main__":
    path =  os.getcwd()
    file_source = path   + r'\source_ip\source_ip.log'
    
    file_check = path + r'\role_ip.txt'
    check_ip = fetch_check_ip(file_check)
    
    adict = {}
    adict = fetch_source_ip(file_source)
    start = time.clock()
    result_dict = {}
    for item in check_ip:
        if item in adict:
            result_dict[item] = adict[item]
    end = time.clock()
    
    print(len(result_dict))
    
    have_set = set()
    all_set = set()
    for item in check_ip:
        all_set.add(item)
        
    with open('result.txt', 'w')  as fd:  
        for k, v in result_dict.items():
            if k in all_set:
              have_set.add(k)
            fd.write("{}\n{}".format(k, v))
    print("time: {}".format(end - start))
    lack_fetch = all_set - have_set
#    for k, v in result_dict.items():
#        print(k, '\n', v)
#        print('\n\n')
    
    save_to_xls(result_dict, lack_fetch)